package com.songxh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.songxh.common.R;
import com.songxh.pojo.Category;
import com.songxh.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author songxh
 * @create 2022-04-24-18:20
 */
@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        Page pageInfo = new Page(page, pageSize);
        QueryWrapper<Category> queryWrapper=new QueryWrapper<>();

        queryWrapper.orderByAsc("sort");
        categoryService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }


    /**
     * 添加功能
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("新增成功");
    }


    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功");
    }


    @DeleteMapping
    public R<String> delete(long ids){
        //categoryService.removeById(ids);
        categoryService.remove(ids);

        return R.success("删除成功");
    }

    /**
     * 前台首页展示套餐时会用上
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        QueryWrapper<Category> queryWrapper=new QueryWrapper<>();
        if (category.getType()!=null){
            queryWrapper.eq("type",category.getType());
        }
        queryWrapper.orderByAsc("sort");
        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }

}
